const fs = require('fs')
const path = require('path')
const pageRouter = require('./pages.js')
const defaultRoute = 'pages/login' // 第一个页面路由

const getRouter = () => {
  const srcPath = path.resolve(__dirname, './routes')
  const result = fs.readdirSync(srcPath)
  let router = []
  result.forEach(file => {
    if (file !== 'index.js') {
      const routes = require('./routes/' + file)
      router = [...router, ...routes]
    }
  })
  return router
}

const pageList = getRouter()
const firstPage = pageList.find(item => item.path === defaultRoute)
const routeList = pageList.filter(item => item.path !== defaultRoute)
routeList.unshift(firstPage)
pageRouter.pages = routeList

fs.writeFile('pages.json',
  JSON.stringify(pageRouter, null, '\t'),
  err => {
    err ? console.error(err) : console.log('pages.json文件更新成功!')
  }
)